<script setup>

import axios from 'axios';
import { onMounted, ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';

const viewData = ref([]);
const para = ref({
    num: null,
    time:[],
    beginTime:null,
    endTime:null
});

const obj = ref({
    num: null,
    money: null,
    reason: null,
    time: null,
    auditor: null,
    state: null
});

const isUpdate = ref(false);
const showDialog = ref(false);
const pageInfo = reactive({
    pageNum: 1,
    pageSize: 5,
    total: 0
});

let getList = () => {
    axios({
        method: "POST",
        url: `/api/penalty/list`,
        data: {
            beginTime :(para.value.time && para.value.time.length > 0)? para.value.time[0] : null,
            endTime :(para.value.time && para.value.time.length > 1)? para.value.time[1] : null,
            num : para.value.num,
            ...pageInfo
        }
    }).then((resp) => {
        if (resp.data.code === 200) {
            viewData.value = resp.data.data.list;
            pageInfo.total = resp.data.data.count ? resp.data.data.count : 0;
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    })
};

const selectobj = () => {
    getList();
};

const addobj = () => {
    obj.value.num = null;
    obj.value.money = null;
    obj.value.reason = null;
    obj.value.time = null;
    obj.value.auditor = null;
    obj.value.reason = null;

    isUpdate.value = false;
    showDialog.value = true;
};

const handleUpdate = (row) => {
    obj.value.num = row.num;
    obj.value.money = row.money;
    obj.value.reason = row.reason;
    obj.value.id = row.id;

    showDialog.value = true;
    isUpdate.value = true;
};

const handleDelete = (row) => {
    obj.value.state = 0;
    obj.value.id = row.id;
    const axiosPara = {
        data: obj.value
    };

    axiosPara.method = "PATCH";
    axiosPara.url = "/api/penalty/update";

    axios(axiosPara).then((resp) => {
        if (resp.data.code === 200) {
            getList();
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    });
};


const submitData = () => {
    const axiosPara = {
        data: obj.value
    };

    if (isUpdate.value) {
        axiosPara.method = "PATCH";
        axiosPara.url = "/api/penalty/update";
    } else {
        axiosPara.method = "POST";
        axiosPara.url = "/api/penalty/add";
    }

    axios(axiosPara).then((resp) => {
        if (resp.data.code === 200) {
            showDialog.value = false;
            getList();
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    });

};

onMounted(() => {
    getList();
});
</script>

<template>
    <div>
        <header class="header">
            <div class="wrapper">
                <div>
                    <span>员工编号:</span>
                    <el-input v-model="para.num" size="small"></el-input>
                </div>
                <div class="s-block">
                    <span class="s-demonstration">处罚时间</span>
                    <el-date-picker v-model="para.time" value-format="YYYY-MM-DD HH:mm:ss" type="datetimerange"
                        placeholder="Select date and time" size="small" />
                </div>
            </div>
            <div class="w-button">
                <el-button @click="selectobj">查询</el-button>
                <el-button @click="addobj">新增</el-button>
            </div>
        </header>

        <main>
            <el-table :data="viewData">
                <el-table-column prop="num" label="员工编号"></el-table-column>
                <el-table-column prop="money" label="金额"></el-table-column>
                <el-table-column prop="reason" label="原因"></el-table-column>
                <el-table-column prop="time" label="处罚时间"></el-table-column>
                <el-table-column prop="auditor" label="记录员"></el-table-column>
                <el-table-column label="审批">
                    <template #default="scope">
                        <el-button size="small" @click="handleUpdate(scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.row)">撤销</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </main>

        <footer>
            <el-pagination v-model:current-page="pageInfo.pageNum" v-model:page-size="pageInfo.pageSize"
                :page-sizes="[5, 10, 20, 50]" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total"
                @size-change="getList" @current-change="getList" />

        </footer>

        <el-dialog v-model="showDialog">
            <div>
                <span>员工编号:</span>
                <el-input v-model="obj.num"></el-input>
            </div>
            <div>
                <span>金额:</span>
                <el-input v-model="obj.money"></el-input>
            </div>
            <div>
                <span>原因:</span>
                <el-input v-model="obj.reason"></el-input>
            </div>
            <div class="block">
                <span class="demonstration">处罚时间</span>
                <el-date-picker v-model="obj.time" value-format="YYYY-MM-DD HH:mm:ss" type="datetime"
                    placeholder="Pick a Date" />
            </div>
            <el-button @click="submitData">提交</el-button>
        </el-dialog>

    </div>
</template>

<style scoped>
.header {
    display: flex;
    flex-direction: column;
}

.wrapper {
    display: flex;
    flex-direction: row;
}

.wrapper>div {
    display: flex;
    flex-direction: row;
    margin-right: 1vw;
    gap: 0.5vw;
}

.wrapper>div>span {
    flex: 0 0 auto;
    /* flex-basis: auto; */
    /* flex-shrink: 0; */
}

.w-button {
    display: flex;
    flex-direction: row;
}

.w-button>button {
    margin-right: 1vw;
}
</style>